home *** CD-ROM | disk | FTP | other *** search
/ Super PC 27 / Super PC 27 (ClarisWorks y shareware).iso / spc / util / executor / docs / synpaper < prev   
Encoding:
Text File  |  1995-03-18  |  16.8 KB  |  425 lines

  1.         Syn68k:  ARDI's dynamically compiling 68LC040 emulator
  2.  
  3.  
  4. I.  Overview
  5.  
  6. This document is meant to give a concise technical summary of how
  7. syn68k works.
  8.  
  9. "Syn68k", ARDI's 68LC040 emulator, is both highly portable and
  10. fast.  The portable core of syn68k, which works by dynamically
  11. compiling 680x0 code into an efficient interpreted form, was designed
  12. to run on all major CPU's.  On supported architectures, syn68k can
  13. also translate 680x0 code into native code that the host processor
  14. can run directly.
  15.  
  16.  
  17. II.  Syngen
  18.  
  19. ARDI's "syngen" system analyzes a lisp-like file describing the
  20. bit patterns and semantics of the 680x0 instruction set and produces
  21. lookup tables and C code for the runtime system to use.  This
  22. process takes place only when syn68k is built, so we can afford
  23. extensive analysis here.  The code and tables generated by syngen
  24. depend somewhat on the characteristics of the host processor; for
  25. example, on a little endian machine it is advantageous to byte swap
  26. some extracted 680x0 operands at compile time instead of at runtime.
  27.  
  28. The 680x0 description file can describe multiple ways to emulate
  29. any particular 680x0 opcode.  The runtime system looks at what CC
  30. bits are live after the instruction and chooses the fastest variant
  31. it can legally use.  In the following example, we have two CC
  32. variants of lsrw; one computes no CC bits, and the other computes
  33. all of them:
  34.  
  35.  
  36. (defopcode lsrw_ea
  37.   (list 68000 amode_alterable_memory () (list "1110001011mmmmmm"))
  38.   (list "-----" "-----" dont_expand
  39.     (assign $1.muw (>> $1.muw 1)))
  40.   (list "CN0XZ" "-----" dont_expand
  41.     (list
  42.      (assign ccx (assign ccc (& $1.muw 1)))
  43.      (ASSIGN_NNZ_WORD (assign $1.muw (>> $1.muw 1))))))
  44.  
  45.  
  46. The 680x0 description file can also specify which 680x0 operands
  47. should be "expanded" to become implicitly known by the corresponding
  48. synthetic opcode.  For example, fully expanding out "addl dx,dy"
  49. would result in 64 synthetic opcodes, one for each combination of
  50. data register operands.  This results in smaller and faster synthetic
  51. opcodes at the expense of increasing the total number of synthetic
  52. opcodes.  To conserve space, we only expand out common 680x0 opcodes.
  53. On host architectures where we can compile to native code, we don't
  54. waste space by "expanding out" common synthetic opcodes.
  55.  
  56.  
  57. III.  Test suites
  58.  
  59. ARDI has a large set of test suites that try thousands upon thousands
  60. of variations of 680x0 opcodes and compare the results to those
  61. generated by a real 68040.  These test suites have proven to be an
  62. invaluable debugging tool, both as new features are added and as
  63. we have ported syn68k to other architectures (notably 80x86, 680x0,
  64. i860 and Alpha).  Our native code support is so recent that our
  65. test suites do not yet adequately test all of the situations that
  66. arise when generating native code, but we plan to extend them in
  67. the near future.
  68.  
  69.  
  70. IV.  Interpreted code
  71.  
  72. Our interpreted code consists of contiguous sequences of "synthetic
  73. opcodes" and their operands.  Syngen can generate ANSI C, but when
  74. compiled with gcc it uses C language extensions that make synthetic
  75. opcodes pointers to the C code responsible for interpreting that
  76. opcode.  This "threaded interpreting" entirely eliminates switch
  77. dispatch and loop overhead.
  78.  
  79. To illustrate the above points, here is the assembly language
  80. generated for the synthetic opcode that would handle a fully expanded
  81. "addl d0,d1" when no CC bit values are required.  This is what
  82. gcc's 80x86 output looks like (edited for readability) after we
  83. run our interpreter through ARDI's Pentium-specific instruction
  84. scheduling Perl script:
  85.  
  86.     movl    _d0,%eax        ; fetch d0
  87.     movl    (%esi),%edi     ; fetch next synthetic opcode
  88.     addl    %eax,_d1        ; do the add
  89.     addl    $4,%esi         ; increment synthetic PC
  90.     jmp     *%edi           ; jump to next synthetic opcode handler
  91.  
  92. We must emphasize that the preceding example is not native code
  93. generated by our emulator, but merely a snippet of what gcc generates
  94. for our interpreter.  This gives you some idea of the efficiency
  95. of the portable component of our emulator.
  96.  
  97.  
  98.  V.  Native code
  99.  
  100. Syn68k supports optional architecture-specific native code extensions.
  101. On systems where they are present, the runtime system tries to
  102. generate native code whenever possible.  In those rare cases when
  103. it cannot, it reverts to our interpreted code.  Since syn68k supports
  104. both native and synthetic code, the runtime system automatically
  105. inserts gateways between the two whenever there is a transition.
  106. This approach allows us to gradually phase in native code handlers
  107. for most 680x0 instructions while leaving tricky and unimportant
  108. rare cases alone.
  109.  
  110. Although our native code compilation engine is not architecture-specific,
  111. to date we have only implemented an 80x86 back end.  The 80x86
  112. architecture has achieved such important status in the industry
  113. that it makes sense for us to describe how we generate native code
  114. for it, even though many of these techniques would not be necessary
  115. on RISC architectures.
  116.  
  117. We are glad that we implemented the most difficult back end first.
  118. We believe that, were we to have started with a RISC back end, we
  119. would have quite possibly architected a system where retrofitting
  120. the exotic mechanisms necessary for efficient 80x86 support was
  121. difficult.
  122.  
  123. Three major problems make translating 680x0 code to 80x86 code difficult:
  124.  
  125. 1) The 80x86 has only 8 registers, while the 680x0 has 16.
  126. 2) The 80x86 is little endian, while the 680x0 is big endian.
  127. 3) The 80x86 does not have general-purpose postincrement and predecrement
  128.    operators, which are used frequently in 680x0 code.
  129.  
  130. On the other hand, several factors make the job easier:
  131.  
  132. 1) The 80x86 has all of the CISC addressing modes commonly used in 680x0 code.
  133. 2) The 80x86 has CC bits that map directly to their 680x0 counterparts
  134.    (except for the 680x0's X bit).
  135. 3) The 80x86 supports 8-, 16- and 32-bit operations, (although it can only
  136.    support 8 bit operations on four of its registers).
  137. 4) The 80x86 and 680x0 have analagous conditional branch instructions.
  138. 5) The 80x86 allows unaligned memory accesses without substantial overhead.
  139.  
  140. The toughest problem is the lack of registers.  On 32-register RISC
  141. architectures it's easy to allocate one RISC register for each
  142. 680x0 register, but on the 80x86 a different approach is needed.
  143. The obvious solution is to perform full-blown inter-block register
  144. allocation, but we fear that using traditional compiler techniques
  145. would be unacceptably slow.
  146.  
  147. For now, we have adopted a simple constraint: between basic blocks,
  148. all registers and live CC bits must reside in their canonical home
  149. in memory.  Within a block, anything goes.  So what liberties does
  150. syn68k take within a block?
  151.  
  152. The 80x86 register set is treated as a cache for recently used
  153. 680x0 registers, and the 80x86 CC bits are used as a cache for the
  154. 680x0 CC bits.  At any particular point within a block, each 680x0
  155. register is either sitting in its memory home or is cached in an
  156. 80x86 register, and each live 680x0 CC bit is either cached in its
  157. 80x86 equivalent or stored in its memory home.  Cached registers
  158. may be in canonical form, may be byte swapped, may have only their
  159. low two bytes swapped, or may be offset by a known constant from
  160. their actual value.
  161.  
  162. Each 680x0 instruction can require that 680x0 registers be cached
  163. in particular ways; the compilation engine generates the minimal
  164. code needed to satisfy those constraints and then calls a sequence
  165. of routines to generate the native code.  As each 680x0 instruction
  166. is processed, each 680x0 register's cache status is updated.  Dirty
  167. registers are canonicalized and spilled back to memory at the end
  168. of each block (or when we run out of 80x86 registers and we need
  169. to make room).
  170.  
  171. We allow 680x0 registers to be cached with varying byte orders and
  172. offsets so that we can perform the optimizations of lazy byte
  173. swapping and lazy constant offsetting.  If the 680x0 program loads
  174. a register from memory and then ends up writing it out later, we
  175. avoid unnecessary byte swaps by not canonicalizing the value
  176. immediately.  Lazy constant offsetting mitigates the overhead of
  177. postincrement and predecrement side effects.  For example, this
  178. 680x0 code:
  179.  
  180.     pea             0x1
  181.     pea             0x2
  182.     pea             0x3
  183.     pea             0x4
  184.     ...
  185.  
  186. becomes this 80x86 code:
  187.  
  188.     movl    _a7,%edi
  189.     movl    $0x01000000,-4(%edi)    ; "push" big-endian constant
  190.     movl    $0x02000000,-8(%edi)
  191.     movl    $0x03000000,-12(%edi)
  192.     movl    $0x04000000,-16(%edi)
  193.     ... <more uses of a7 may follow, and they'll use %edi>
  194.     subl    $16,%edi
  195.     movl    $edi,_a7
  196.     ...
  197.  
  198.  
  199. As mentioned above, we use the 80x86 condition code bits as a cache
  200. for the real 680x0 CC bits.  Although live cached CC bits are
  201. occasionally spilled back to memory because some 80x86 instruction
  202. is about to clobber them, this trick almost always works.  Using
  203. 80x86 CC bits, we can frequently get away with extremely concise
  204. code sequences; for example, a 680x0 compare and conditional branch
  205. becomes an 80x86 compare and conditional branch.
  206.  
  207.  
  208. VI.  Self-modifying code
  209.  
  210. Like most dynamically compiling emulators, syn68k doesn't detect
  211. self-modifying code; the overhead is too high.  Fortunately,
  212. self-modifying programs don't work on the real 68040 either.  We
  213. rely on the program making explicit system calls to flush the caches
  214. whenever 680x0 code may have been modified or created.  Some programs
  215. (like HyperCard) flush the caches very often, which can cause real
  216. performance headaches if code is continuously recompiled.  We have
  217. solved this problem by checksumming 680x0 blocks as they are compiled
  218. and only decompiling blocks which fail their checksums.  This
  219. optimization alone sped up some HyperCard stacks by a factor of
  220. three or so.
  221.  
  222.  
  223.  
  224. VII.  Responsiveness
  225.  
  226. Responsiveness is a concern for any dynamic compiler.  Fortunately,
  227. syn68k is largely driven by automatically generated lookup tables
  228. so compilation speed is good.  Like other dynamic compilers, syn68k
  229. only bothers to compile 680x0 code when it encounters it for the
  230. first time.
  231.  
  232. When syn68k encounters new code, it compiles other 680x0 code that
  233. it can reach from there but does not compile through jsr's.  Only
  234. when a jsr is actually executed does syn68k compile the target
  235. routine.  Once that target routine is compiled, syn68k modifies
  236. the jsr handler to point directly to the target routine so that
  237. the jsr will be extremely fast the second time it is executed.
  238. We've found that lazily compiling through jsr's does a good job of
  239. avoiding compilation lag that might annoy the user.
  240.  
  241. Syn68k does not attempt to generate native code for a basic block
  242. until that block (or a nearby one) has been executed 50 times.
  243. This saves memory and some compilation time, although we haven't
  244. noticed any particular sluggishness when compiling to native code.
  245.  
  246.  
  247. VIII.  Other optimizations
  248.  
  249. Syn68k maintains an internal "jsr stack" to speed up the common
  250. case of jsr/rts.  We realize that the rts address might have been
  251. fiddled with, so the rts handler verifies at runtime that the rts
  252. address matches the tag on top of the jsr stack.  If it matches,
  253. syn68k does a fast jump.  If it doesn't, syn68k looks up the code
  254. corresponding to the rts address in a hash table.
  255.  
  256.  
  257. IX.  Neat hacks
  258.  
  259. The low-level code generation routines for the 80x86 back end are
  260. machine generated from assembly language templates.  Thousands of
  261. operand permutations for 80x86 instructions of interest are run
  262. through the system's assembler and analyzed to derive the rules
  263. the assembler uses to create binaries.  Those rules are encapsulated
  264. into C code and compiled into syn68k so we can generate binaries
  265. on the fly.  Here is a sample template:
  266.  
  267.   { "i386_leal_indoff", "", "", "", "", "-",
  268.       "leal %0(%1),%2",
  269.       { "offset", "base", "dst" },
  270.       { { SIZE_32, CONSTANT, IN }, { SIZE_32, REGISTER, IN },
  271.         { SIZE_32, REGISTER, OUT } } },
  272.  
  273. This approach has saved us countless hours of debugging and allows
  274. our system to automatically perform the same optimizations as the
  275. host system's assembler.
  276.  
  277. We've annotated our 80x86 descriptions with information about
  278. Pentium pairability so that future versions of syn68k can schedule
  279. the native code we generate (we already schedule our main interpreter
  280. when we build syn68k).
  281.  
  282.  
  283. X.  Future optimizations
  284.  
  285. We are working on a simple inter-block register allocation algorithm.
  286.  
  287. By relocating most 680x0 register->80x86 register moves to the
  288. beginning of each block, we can improve Pentium pairability and
  289. reduce 80486 and Pentium address generation pipeline stalls.
  290.  
  291. Now that we compile to native code, A-line trap overhead is becoming
  292. significant.  We may soon compile A-line traps to native code that
  293. directly calls the appropriate ROMlib routine with the appropriate
  294. arguments (checking at runtime to make sure that neither the trap
  295. nor the A-line vector has been patched out, of course).
  296.  
  297.  
  298. XI.  Code examples
  299.  
  300. Here are two sample 680x0 code sequences from real applications,
  301. and the 80x86 code that syn68k generates for them.  We chose these
  302. code sequences specifically to showcase several of the techniques
  303. we use, so you shouldn't use them as a substitute for benchmarks.
  304. Not all 680x0 code translates as well as these examples do, but
  305. these examples are far from exotic.
  306.  
  307.  
  308. Example 1 (Solarian):
  309.  
  310. 680x0 code:
  311.  
  312.     addqb   #1,a4@(1)
  313.     movel   #0,d0
  314.     moveb   a4@,d0
  315.     swap    d0
  316.     clrw    d0
  317.     swap    d0
  318.     asll    #2,d0
  319.     lea     a5@(-13462),a0
  320.     addal   d0,a0
  321.     moveal  a0@,a0
  322.     movel   #0,d0
  323.     moveb   a4@(1),d0
  324.     cmpw    a0@,d0
  325.     bcs     0x3fffee2
  326.  
  327.  
  328. 80x86 code:
  329.  
  330.     movl    _a4,%edi                ; addqb #1,a4@(1)
  331.     addb    $0x1,0x1(%edi)
  332.     xorl    %ebx,%ebx               ; movel #0,d0
  333.     movb    (%edi),%bl              ; moveb a4@,d0
  334.     rorl    $0x10,%ebx              ; swap d0
  335.     xorw    %bx,%bx                 ; clrw d0
  336.     rorl    $0x10,%ebx              ; swap d0
  337.     shll    $0x2,%ebx               ; asll #2,d0
  338.     movl    _a5,%esi                ; lea a5@(-13462),a0
  339.     leal    0xffffcb6a(%esi),%edx
  340.     addl    %ebx,%edx               ; addal d0,a0
  341.     movl    (%edx),%edx             ; moveal a0@,a0
  342.     xorl    %ebx,%ebx               ; movel #0,d0
  343.     movb    0x1(%edi),%bl           ; moveb a4@(1),d0
  344.     bswap   %edx                    ; cmpw a0@,d0
  345.     movw    (%edx),%cx
  346.     rorw    $0x8,%cx
  347.     cmpw    %cx,%bx
  348.     movl    %edx,_a0                ; <spill dirty 68k
  349.     movl    %ebx,_d0                ;  registers back to memory>
  350.     jb      0x6fae0c                ; bcs 0x3fffee2
  351.     jmp     0x6faf0c                ; <go to "fall through" code>
  352.  
  353.  
  354.  
  355.  
  356. Example 2 (PageMaker):
  357.  
  358. 680x0 code:
  359.  
  360.     movel   #0,d2
  361.     moveb   d0,d2
  362.     lslw    #8,d0
  363.     orw     d0,d2
  364.     movel   d2,d0
  365.     swap    d2
  366.     orl     d2,d0
  367.     movel   a0,d2
  368.     lsrb    #1,d2
  369.     bcc     0x3fffed4
  370.  
  371. 80x86 code:
  372.  
  373.     xorl    %ebx,%ebx               ; movel #0,d2
  374.     movl    _d0,%edx                ; moveb d0,d2
  375.     movb    %dl,%bl
  376.     shlw    $0x8,%dx                ; lslw #8,d0
  377.     orw     %dx,%bx                 ; orw d0,d2
  378.     movl    %ebx,%edx               ; movel d2,d0
  379.     rorl    $0x10,%ebx              ; swap d2
  380.     orl     %ebx,%edx               ; orl d2,d0
  381.     movl    _a0,%ecx                ; movel a0,d2
  382.     movl    %ecx,%ebx
  383.     shrb    %bl                     ; lsrb #1,d2
  384.     movl    %ebx,_d2                ; <spill dirty 68k
  385.     movl    %edx,_d0                ;  registers back to memory>
  386.     jae     0x3b734c                ; bcc 0x3fffed4
  387.     jmp     0x43d48c                ; <go to "fall through" 68k code>
  388.  
  389.  
  390. XII.  Benchmarks
  391.  
  392. These performance numbers were computed with Speedometer 3.23.
  393. We've removed the floating point tests from the list since they do
  394. not measure syn68k's speed.  Syn68k contains no special provisions
  395. for Speedometer's benchmarks and we believe that these numbers are
  396. indicative of syn68k's performance for many other CPU-intensive
  397. programs.
  398.  
  399.  
  400.         Quadra  Pentium 486DX4  486DX/2
  401.           610    90MHz   75MHz   66MHz
  402.         ------  ------  ------  ------
  403. CPU             16.018  28.833  15.727  13.840
  404.  
  405. Dhrystones      19.586  21.886  12.084   9.424
  406. Tower           18.909  27.130  12.235  11.556
  407. Quicksort       17.759  27.105  15.606  13.919
  408. Bubble sort     18.409  31.154  19.286  16.875
  409. Queens          19.083  38.167  19.083  18.320
  410. Puzzle          22.083  44.167  23.661  21.032
  411. Permutations    21.019  28.564  11.604  12.242
  412. Int. Matrix     24.200  26.469  19.369  16.608
  413. Sieve           23.362  60.290  33.982  30.145
  414.         ------  ------  ------  ------
  415. Average         20.490  33.881  18.582  16.680
  416.  
  417.  
  418.  
  419. Preliminary analysis suggests that we average a roughly 3:1
  420. instruction count increase when translating to 80x86 code.  We have
  421. not yet taken rigorous measurements, but the 3:1 figure is lent
  422. some credence by the fact that our 75MHz 486DX4 gets nearly the
  423. same performance as our Quadra 610.  We believe that inter-block
  424. register allocation will noticeably improve this ratio.
  425.